package com.statistics.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.statistics.bean.Environment;
import com.statistics.bean.EnvironmentTower;
import com.statistics.cloudpower.dao.TblEnvironmentStatisticsWeekDao;
import com.statistics.cloudpower.dao.TblEnvironmentTowerStatisticsDayDao;
import com.statistics.cloudpower.dao.TblEnvironmentTowerStatisticsMonthDao;
import com.statistics.cloudpower.dao.TblEnvironmentTowerStatisticsWeekDao;
import com.statistics.cloudpower.entity.*;
import com.statistics.tools.Constants;
import com.statistics.tools.Tools;
import com.statistics.yfdata.dao.DataEnvironmentRepository;
import com.statistics.yfdata.dao.DataEnvironmentTowerRepository;
import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author MeowParse
 * @date 2017/9/2.
 */
public class EnvironmentTowerStatisticsWeekThread extends Thread {

	private Logger logger;
	private String uuid;
	private List<TblEquipment> obj;
	private int fragment_number;
	private TblEnvironmentTowerStatisticsWeekDao tblEnvironmentTowerStatisticsWeekDao;
	private DataEnvironmentTowerRepository dataEnvironmentTowerRepository;

	public EnvironmentTowerStatisticsWeekThread(List<TblEquipment> obj,
	                                            TblEnvironmentTowerStatisticsWeekDao dao, String uuid, int fragment_number,
	                                            DataEnvironmentTowerRepository dataEnvironmentTowerRepository){
		this.obj = obj;
		this.fragment_number = fragment_number;
		this.uuid = uuid;
		this.dataEnvironmentTowerRepository = dataEnvironmentTowerRepository;
		this.tblEnvironmentTowerStatisticsWeekDao = dao;
		this.logger = Logger.getLogger("环境塔周统计");
	}

	@Override
	public void run() {

		//根据读取的command进行相应运算
		try {
			DateFormat dataFormat = DateFormat.getDateInstance();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String thread_start_time = simpleDateFormat2.format(new Date());

			String now = dataFormat.format(new Date());
			Date today = simpleDateFormat.parse(now);
			Long start_date;//前一天起始时间的时间戳
			Long end_date;//前一天结束时间的时间戳

			List<TblEquipment> tblEquipment = obj;
			List records = null;
			int equipment_number;

			//每周电量统计
			start_date = today.getTime() - 24 * 3600 * 1000 * 7;
			end_date = today.getTime();

			logger.info(start_date);
			logger.info(end_date);

			records = new ArrayList<TblElectricityStatisticsWeek>();

			if (records == null) {
				return;
			}

			//对这n条设备数据逐条进行查找相应的电量列表
			for (equipment_number = 0; equipment_number < tblEquipment.size(); equipment_number++) {
				JSONArray jsonArray = new JSONArray();

				if (tblEquipment.get(equipment_number) == null) {
					continue;
				}

				//取得头尾数据差值
				String mac = tblEquipment.get(equipment_number).getMac();

				List<EnvironmentTower> environmentTowers = dataEnvironmentTowerRepository.getCountByWeek(mac, start_date, end_date);

				if (environmentTowers == null) {
					//没有这条设备记录
					continue;
				}

				//环境塔数据保存
				for (EnvironmentTower environment :environmentTowers) {

					TblEnvironmentTowerStatisticsWeek one_record = new TblEnvironmentTowerStatisticsWeek();
					one_record.setMac(mac);
					BeanUtils.copyProperties(environment, one_record);
					one_record.setStatisticsTime(new Timestamp(start_date));
					records.add(one_record);

					JSONObject jsonObject = Tools.getJsonEnvTowStr(mac,environment,start_date);

					jsonArray.add(jsonObject);

				}

				Tools.sendTo61(Constants.DIANBIAODAY,jsonArray.toJSONString());

			}

			tblEnvironmentTowerStatisticsWeekDao.save(records);

			records.clear();

			String thread_end_time = simpleDateFormat2.format(new Date());
			logger.info(uuid + " 第" + fragment_number + "分片 Count=" + equipment_number + " Start=" + thread_start_time + " End=" + thread_end_time);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}

}
